$(document).ready(function () {
    $('#binding_button').on('click', function () {
        doBinding();
    });
    $('#binding_mobile_button').on('click', function () {
        doBindingMobile();
    });
    $('#btn_send_binding').on('click', function () {
        sendBindingCode();
    });
});
//手机绑定发送手机登录码
function sendBindingCode() {
    var mobile = $('#binding_mobile').val();
    var ranCode = $('#binding_ran_code').val();
    if (!isMobile(mobile)) {
        $("#binding_mobile").focus();
        return layer.alert("请输入正确的手机号码！");
    }
    var loading = layer.load(2, {time: 5 * 1000});
    $.ajax({
        url: '/ajax/sendCode.json',
        type: 'post', dataType: 'json',
        data: {"type":"binding","mobile": mobile , "ranCode" : ranCode},
        success: function (result) {
            if (result.code === 0) {
                var timeNum = 60; /*当点击获取验证码后设置60秒计时不可点击*/
                //设置按钮不可点击
                $("#btn_send_binding").addClass("layui-btn-disabled").attr("onclick", "").text(timeNum + "秒后可点击");
                var timeTicket = setInterval(function () {
                    if (timeNum > 1) {
                        timeNum--;
                        $("#btn_send_binding").text(timeNum + "秒后可点击");
                    } else {
                        $("#btn_send_binding").removeClass("layui-btn-disabled").attr("onclick", "sendBindingCode()").text("获取短信码");
                        clearInterval(timeTicket);
                    }
                }, 1000);
            } else {
                return layer.alert(result.msg);
            }
        },
        error: function (e) {
            return layer.alert("send error!");
        },
        complete: function () {
            layer.close(loading);
        }
    });
}

function doBinding() {
    var mobile = $('#binding_mobile').val();
    var mobileCode = $('#binding_mobile_code').val();
    if (!isMobile(mobile)) {
        $("#binding_mobile").focus();
        return layer.alert("请输入正确的手机号码！");
    }
    if (mobileCode === "") {
        $("#binding_mobile_code").focus();
        return layer.alert("请输入手机接收到的绑定码！");
    }
    var loading = layer.load(2);
    $.ajax({
        url: '/auth/wechat/binding',
        type: 'post', dataType: 'json',
        data: {"mobile": mobile, "mobileCode": mobileCode},
        success: function (result) {
            if (result.code === 0) {
                location.href = qdxRedirect + "?user_id=" + result.data.userId + "&state=" + qdxState + "&code=" + result.data.code;
            } else {
                return layer.alert(result.msg);
            }
        },
        error: function (e) {
            return layer.alert("send error!");
        },
        complete: function () {
            layer.close(loading);
        }
    });
}


function doBindingMobile() {
    var mobile = $('#binding_mobile').val();
    var mobileCode = $('#binding_mobile_code').val();
    if (!isMobile(mobile)) {
        $("#binding_mobile").focus();
        return layer.alert("请输入正确的手机号码！");
    }
    if (mobileCode === "") {
        $("#binding_mobile_code").focus();
        return layer.alert("请输入手机接收到的绑定码！");
    }
    var loading = layer.load(2);
    $.ajax({
        url: '/user/binding/mobile.json',
        type: 'post', dataType: 'json',
        data: {"mobile": mobile, "mobileCode": mobileCode},
        success: function (json) {
            if (json.code === 0) {
                parent.location.href = "/user/index.html";
            } else {
                return layer.alert(json.msg);
            }
        },
        error: function (e) {
            return layer.alert("send error!");
        },
        complete: function () {
            layer.close(loading);
        }
    });
}